<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/kendocommon.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <title>商品管理</title>
    <meta charset="utf-8">
    <style>
    	table{font-size:12px}
    </style>
    <script>
        $(document).ready(function() {
        	        
        	$("#toolbar").kendoToolBar({
	            resizable: false,
	            items: [
	            	{ type: "separator" },
	                //{ template: toolbarTemp},
					//{ type: "separator" },
	                { template: '<label>旗舰店:</label><input id="park" />'},
	                { type: "separator" },
	                { template: '<label>商品名:</label><input id="productName" class="k-textbox"/>'},
	                { type: "separator" },
	                { type: "button", text: "查询",click: queryCustomerList },
	                { type: "button", text: "保存",click: saveParkProduct },
	            ]
	        });
	        
          	parkDataSource = new  kendo.data.DataSource({
            	transport: {
                    read:  {
                        url: "${path}/commonSel/getParkSel",
                        dataType: "json"
                    }
                }
            });
            
        	$("#park").kendoComboBox({
			    dataTextField: "name",
			    dataValueField: "id",
			    dataSource: parkDataSource,
			    value:"${kids_parkID}"
			});
			
			//var combobox = $("#park").data("kendoComboBox");
			//combobox.select(function(dataItem) {
			//   return dataItem.id == "51c7325f-3c91-484f-85d0-1feecef70116";
			//});

	        
            dataSource = new kendo.data.DataSource({
                transport: {
                    read:  {
                        url: "${path}/assist/queryProduct",
                        dataType: "json",
                        data: function() {
					        return {
					         	kids_park_id:$("#park").data("kendoComboBox").value(),
					         	productName:$("#productName").val()
					        };
					    }
                    }
                },
                //group: [{field: "businessTypeCode"}],
                serverPaging: true,
                serverFiltering: true,
                pageSize: 15,
                schema: {
                	data:"data",
               		total:"total",
               		model: {
				        fields: {
				          checked: {editable: false},
				          businessTypeCode: {editable: false},
				          name: {editable: false},
				          proname: {editable: false},
				          unitPrice: {editable: false},
				          parkPrice: {type: "number"},
				          isDiscount: {editable: false}
				        }
				    }
                }
            });
            
            $("#grid").kendoGrid({
                dataSource: dataSource,
                //pageable: true,
                pageable: {
					refresh: true,
					pageSizes: true,
					buttonCount: 5,
					page: 1,
					pageSize: 10,
					pageSizes: [10, 20, 30],
					messages: {
						display: "显示 {0}-{1} 共 {2} 项",
						empty: "没有数据",
						itemsPerPage: "每面显示数量",
						first: "第一页",
						last: "最后一页",
						next: "下一页",
						previous: "上一页"
					}
				},
                selectable: "multiple row",
                allowCopy: true,
                //groupable: {
                //    messages: {
                //      empty: "将列拖拽到此处进行分类查看"
                //    }
                //},
                editable: 'incell',
                columns: [
                	{ field:"checked", template: "<input type='checkbox' class='gridchk' #=checked?checked='checked':'' #/>",width: "40px",
                		headerTemplate: '<input type="checkbox" id="check-all"/>',editable: false},
                	{ command: [{name:"editrow",text:"<span class='k-icon k-edit'></span>删除",click:removeProcudt,buttonType:"ImageAndText"}], title: "操作", width: "80px" },
                	{ field:"businessTypeCode", title: "业务代码",width: "150px",editable: false},
                    { field:"name", title: "业务名",width: "150px",editable: false},
                    { field:"proname", title: "商品名",width: "250px",editable: false},
                    { field:"unitPrice", title: "原价格",width: "250px",editable: false},
                    { field:"parkPrice", title: "乐园价格",width: "250px"},
                    { field:"isDiscount", title: "是否可打折",width: "120px",template: "<input type='checkbox' class='gridDiscountChk' #=isDiscount?checked='checked':'' #/>",editable: false}
                    //{ field: "UnitsInStock", title:"Units In Stock", width:"120px"},//,format:"{0:yyyy/MM/dd}"
                    //{ command: [{name:"editrow",text:"<span class='k-icon k-edit'></span>查看",click:queryCustomerInfo,buttonType:"ImageAndText"}], title: "操作", width: "100px" }
                ],
                dataBound: function (e) {
                    var grid = e.sender;
                    var dataItems = grid.dataSource.data();
                    $.each(grid.items(), function (i, item) {
                        var curDataItem = dataItems[i];
                        if(curDataItem){
                        	if(curDataItem.checked==0){
                        		$(item).find(".k-grid-editrow").remove();
                        	}
                        }
                    });
                }
            });
            
            $('#check-all').change(function(){
            	var grid = $("#grid").data("kendoGrid");
            	$.each(grid.items(), function (i, item) {
					var cbxController = $(item).find(".gridchk");
					var isChecked = cbxController.is(":checked");
					if(isChecked){
						cbxController.prop("checked",false); 
					}else{
						cbxController.prop("checked",true); 
					}
				});
            });
            
            $('#grid').css("height",document.body.clientHeight-40);
            //$("#isMember").kendoComboBox();
            
		});
        
        var removeProcudt = function(e){
			var grid = $("#grid").data("kendoGrid");
			e.preventDefault();
            var data = grid.dataItem($(e.currentTarget).closest("tr"));
            layer.confirm('确定删除?', function(index){
	           	$.ajax({
	   		        url: "${path}/assist/removeParkProduct",
	   		        //dataType: 'json',
	      				//contentType: "application/json",
	   		        data: {park_id:data.kids_parkID,product_id:data.product_id},
	   		        type:"POST", 
	   		        success: function (text) {
	   		        	$("#grid").data("kendoGrid").dataSource.fetch();
	   		        	toastr.info(text.msg, "提示", toastrOptsBottom);
	   		        },
	   		        error: function (jqXHR, textStatus, errorThrown) {
	   		        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
	   		        }
	   		    });
           	  	layer.close(index);
           	});       
		}
		
		var queryCustomerList = function(e){//查询
			var grid = $("#grid").data("kendoGrid");
			var checkedIDs = new Array();
			$.each(grid.items(), function (i, item) {
				var cbxController = $(item).find(".gridchk");
				var isChecked = cbxController.is(":checked");
				var curData = grid.dataItem(item);
				if (isChecked){
					checkedIDs.push(curData.phone);
					//console.log(curData.phone);
					//console.log(curData);
				}
			});
			
            //var selectedRowIds = $("#grid").attr('data-sympleTech-KendoGrid-selected');
	        //console.log(selectedRowIds);
            
			//$("#grid").data("kendoGrid").dataSource.read({isMember:$("#isMember").data("kendoComboBox").value()});
			$("#grid").data("kendoGrid").dataSource.fetch();
			$("#grid").data("kendoGrid").dataSource.page(1);
		}
		
		var saveParkProduct = function(e){
			var grid = $("#grid").data("kendoGrid");
			var checkedArr = new Array();
			checkedArr.push({parkid:$('#park').val()});//设置乐园编号(parkID)
			var model = {};
			$.each(grid.items(), function (i, item) {
				var cbxController = $(item).find(".gridchk");
				var isChecked = cbxController.is(":checked");
				
				var cbxDisController = $(item).find(".gridDiscountChk");
				var isDisChecked = cbxDisController.is(":checked");
				var curData = grid.dataItem(item);
				if(isDisChecked){
					curData.isDiscount = 1;
				}else{
					curData.isDiscount = 0;
				}
				if (isChecked){
					model.id = curData.id;
					model.parkPrice = curData.parkPrice;
					//checkedArr.push({id:curData.id,parkPrice:curData.parkPrice});
					checkedArr.push(curData);
				}
			});
			
			$.ajax({
		        url: "${path}/assist/saveParkProduct",
		        dataType: 'json',
   				contentType: "application/json",
		        data: JSON.stringify(checkedArr),
		        type:"POST", 
		        success: function (text) {
		        	$("#grid").data("kendoGrid").dataSource.fetch();
		        	toastr.info(text.msg, "提示", toastrOptsBottom);
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		        	toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
		        }
		    });
		}
		
		var toolbarTemp = function(dataItem){
			return "<label>时间:</label><input name='querydate' id='querydate' class='k-textbox'>";
		}
		
    </script>
</head>
<body>
	<div id="toolbar" style="margin:2px"></div>
    <div id="grid" style="margin:2px;font-size:10px"></div>
</body>
</html>
